from fastapi import APIRouter
from fastapi import Request
from service.PatientService import PatientService
from util.MyExceptionHandler import MyExceptionHandler
import inspect
import json
from util import Logger
from StatusCode import StatusCode

router = APIRouter(tags=["患者管理路由"])


@router.get("/patient/list")
def getPatientList(request: Request):
    try:
        json_data = dict(request.query_params)
        data = PatientService().getPatients(json_data)
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records"),
            "total": data.shape[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/patient/names")
def getPatientNames(request: Request):
    try:
        data = PatientService().getPatientNames()
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records"),
            "total": data.shape[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/patient/info")
def getPatientInfo(request: Request):
    try:
        json_data = dict(request.query_params)
        data = PatientService().getPatientInfo(json_data)
        if data.empty:
            response = {
                "code": StatusCode.RESPONSE_FAIL,
                "msg": "获取患者信息失败,请检查数据后重试",
            }
            return response
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records")[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/patient/update")
async def updatePatient(request: Request):
    try:
        json_data = await request.json()
        flag = PatientService().updatePatient(json_data)
        response = {"code": flag}
        if flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "修改患者信息失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/patient/add")
async def addPatient(request: Request):
    try:
        json_data = await request.json()
        flag = PatientService().addPatient(json_data)
        response = {"code": flag}
        if flag == StatusCode.RESPONSE_EXIST:
            response["msg"] = "添加患者失败,患者已存在"
        elif flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "添加患者失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/patient/del")
def deletePatient(request: Request):
    try:
        json_data = dict(request.query_params)
        flag = PatientService().delPatient(json_data)
        response = {"code": flag}
        if flag != StatusCode.RESPONSE_SUCCESS:
            response["msg"] = "删除患者失败,请检查数据后重试"
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response
